<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入医生名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :inline="true" :model="patientform" class="demo-form-inline">
          <el-form-item label="用户名称">
            <el-input v-model="patientform.name" placeholder="请输入用户名称"></el-input>
          </el-form-item>
          <el-form-item label="电话号码">
            <el-input v-model="patientform.phone" placeholder="请输入电话号码"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="patientform.status" placeholder="用户状态">
              <el-option label="正常" value="0"></el-option>
              <el-option label="停用" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="patientform.startTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择起始日期">
            </el-date-picker>
            <el-date-picker
              v-model="patientform.endsTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择截止日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>
        <el-button @click="newPatient">新增</el-button>
        <el-table :data="patientList">
          <el-table-column label="患者编号" prop="id" align="center"></el-table-column>
          <el-table-column label="患者名称" prop="name" align="center"></el-table-column>
          <el-table-column label="手机号码" prop="phone" align="center"></el-table-column>
          <el-table-column label="备注" prop="remark" align="center"></el-table-column>
          <el-table-column label="状态" prop="status" align="center"></el-table-column>
          <el-table-column label="创建时间" prop="date" align="center"></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button @click="deletePatient(scope.row.id)">删除</el-button>
              <el-button @click="update(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog
          :title="title"
          :visible.sync="dialogVisible"
          width="25%">
          <el-input type="hidden" v-model="patient.id"></el-input>
          患者名称：<el-input type="text" v-model="patient.name" style="width: 300px"></el-input><br><br>
          手机号码：<el-input type="text" v-model="patient.phone"style="width: 300px"></el-input><br><br>
          备注：<el-input type="text" v-model="patient.remark"style="width: 300px"></el-input><br><br>
          状态：<el-input type="text" v-model="patient.status"style="width: 300px"></el-input><br><br>

          <div v-if="title=='新增患者'">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addPatient">确 定</el-button>
          </div>
          <div v-else>
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="updatePatient">修 改</el-button>
          </div>
        </el-dialog>

      </el-col>
    </el-row>
  </div>
</template>

<script>
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getUserId,ListPatient,addPatient,updatePatient,deletePatient} from '@/api/system/patient';


export default {
  name: "Patient",
  components: { Treeselect },
  data() {
    return {
      patientform: {
        name: '',
        phone:'',
        status: 0,
        startTime:'',
        endsTime:'',
        doctId:0,

      },
      patient:{
        doctId:4,
        name:'',
        phone:'',
        remark:'',
        status:0,
        id:'',
      },
      patientList:[],
      title:'新增患者',
      dialogVisible:false,

      user:{
        id:0
      },
      user1:[],
      id:0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 默认密码
      initPassword: undefined,
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getConfigKey("sys.user.initPassword").then(response => {
      this.initPassword = response.msg;
    });

  },
  methods: {
    getList() {
      ListPatient(this.patientform).then(response => {
        console.log(response);
          this.patientList = response;
        }
      );
    },
    newPatient:function () {
      this.title="新增患者";
      this.dialogVisible=true;
    },
    addPatient:function () {
      console.log(this.patient);
      //js 增加病人方法
      addPatient(this.patient);
      //查询所有
      this.getList();
      this.dialogVisible=false;
    },
    deletePatient: async function (stuid) {
      var stu = {id: stuid};
      await deletePatient(stu);
      //查询所有
      this.getList();
    },
    update: function (s) {
      this.patient =s;
      this.title="修改患者";
      this.dialogVisible=true;
    },
    updatePatient:function () {
      console.log(this.patient);
      updatePatient(this.patient);
      //查询所有
      this.getList();
      this.dialogVisible=false;
      console.log("结束修改");
      console.log(this.patient);
    },

    onSubmit() {
      ListPatient(this.patientform).then(response => {
          console.log(response);
          this.patientList = response;
        }
      );
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      console.log(data.id);
      this.user.id=data.id;
      let that=this;
      getUserId(that.user).then(response =>{
        that.user.id=response;
        console.log(this.user.id);
        that.patientform.doctId=response;
        that.patient.doctId=response;
        that.getList();
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
  }
};
</script>
